package com.chain.BrotherExchange.ui.fragment;

import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.support.v7.widget.RecyclerView;

import com.chain.BrotherExchange.C;
import com.chain.BrotherExchange.R;
import com.chain.BrotherExchange.data.Resource;
import com.chain.BrotherExchange.event.UserAssetsChangeEvent;
import com.chain.BrotherExchange.ui.adapter.LegalTenderAssetsListAdapter;
import com.chain.BrotherExchange.ui.adapter.MyAssetsListAdapter;
import com.chain.BrotherExchange.ui.core.AbsNetworkStateFragment;
import com.chain.BrotherExchange.viewmodel.MyAssetsViewModel;
import com.chain.BrotherExchange.viewmodel.core.ViewModelProviders;

import org.greenrobot.eventbus.Subscribe;

import butterknife.Bind;

/**
 * 我的资产->币币、法币资产
 */
public class UserAssetsListFragment extends AbsNetworkStateFragment {

    @Bind(R.id.recycler_view) RecyclerView mRecyclerView;

    private MyAssetsViewModel mViewModel;
    private int mPageType;

    @Override
    protected int getLayoutID() {
        return R.layout.layout_simple_recycler_view;
    }

    @Override
    protected boolean useEventBus() {
        return true;
    }

    @Override
    protected void initActions() {
        mViewModel = ViewModelProviders.of(this).get(MyAssetsViewModel.class);
        mPageType = getArguments().getInt(C.EXTRA_PAGE_TYPE);
        initAssetsList();
    }

    private void initAssetsList() {
        switch (mPageType) {
            case C.VALUE_COIN_ASSETS_TYPE:
                initCoinAssetsList();
                break;
            case C.VALUE_LEGAL_CURRENCY_TYPE:
                initLegalTenderAssetsList();
                break;
        }
    }

    private void initLegalTenderAssetsList() {
        mViewModel.requestLegalCurrencyAssets().observe(this, resource -> {
            Resource.load(getContext(), resource, this, result ->
                    mRecyclerView.setAdapter(new LegalTenderAssetsListAdapter(result))
            );
        });
    }

    private void initCoinAssetsList() {
        mViewModel.requestMonetaryAssets().observe(this, resource -> {
            Resource.load(getContext(), resource, this, result ->
                    mRecyclerView.setAdapter(new MyAssetsListAdapter(result))
            );
        });
    }

    @Override
    public void onAnewRequest() {
        initAssetsList();
    }

    public static Fragment coinAssetsFragment() {
        UserAssetsListFragment fragment = new UserAssetsListFragment();
        Bundle bundle = new Bundle();
        bundle.putInt(C.EXTRA_PAGE_TYPE, C.VALUE_COIN_ASSETS_TYPE);
        fragment.setArguments(bundle);
        return fragment;
    }

    public static Fragment legalCurrencyFragment() {
        UserAssetsListFragment fragment = new UserAssetsListFragment();
        Bundle bundle = new Bundle();
        bundle.putInt(C.EXTRA_PAGE_TYPE, C.VALUE_LEGAL_CURRENCY_TYPE);
        fragment.setArguments(bundle);
        return fragment;
    }

    @Subscribe
    public void userAssetsChnageEvent(UserAssetsChangeEvent event) {
        initAssetsList();
    }
}
